import index from "./data/index";
import database from "./data/data.json";
const _random = <T extends string>(arr: string[]): T => {
    return arr[Math.floor(Math.random() * arr.length)] as any;
};
type TupleToUnion<T extends readonly unknown[]> = T[number];
export type KaomojiType = TupleToUnion<typeof index>;

export const randomKaomoji = (
    type?: KaomojiType | KaomojiType[],
    random = _random
) => {
    if (type === undefined) {
        type = [random(index as any)] as KaomojiType[];
    }
    if (!(type instanceof Array)) {
        type = [type] as KaomojiType[];
    }
    const picked: string[] = type.flatMap((i) => {
        if (i in database) {
            return (database as any)[i];
        }
        return [];
    });
    return random(picked);
};
